Skip to content

[Flang] Move builtin .mod generation into runtimes#679

Merged
ronlieb merged 16 commits into
amd-stagingfrom
users/meinersbur/rocm_flang_builtin-mods
Jun 9, 2026
Merged

[Flang] Move builtin .mod generation into runtimes#679
ronlieb merged 16 commits into
amd-stagingfrom
users/meinersbur/rocm_flang_builtin-mods

Conversation

@Meinersbur

@Meinersbur Meinersbur commented Nov 25, 2025

Copy link
Copy Markdown

llvm#169638 pre-merged into amd-staging

@github-actions

Copy link
Copy Markdown

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@z1-cciauto

Copy link
Copy Markdown
Collaborator

@z1-cciauto

Copy link
Copy Markdown
Collaborator

@Meinersbur Meinersbur force-pushed the users/meinersbur/rocm_flang_builtin-mods branch from 0f93940 to a41ffe8 Compare November 25, 2025 16:08
@z1-cciauto

Copy link
Copy Markdown
Collaborator

@Meinersbur Meinersbur force-pushed the users/meinersbur/rocm_flang_builtin-mods branch from a41ffe8 to 4a8fef3 Compare December 1, 2025 19:14
@z1-cciauto

Copy link
Copy Markdown
Collaborator

@z1-cciauto

Copy link
Copy Markdown
Collaborator

@Meinersbur Meinersbur marked this pull request as ready for review December 2, 2025 14:42
@z1-cciauto

Copy link
Copy Markdown
Collaborator

@z1-cciauto

Copy link
Copy Markdown
Collaborator

@z1-cciauto

Copy link
Copy Markdown
Collaborator

@Meinersbur

Meinersbur commented Dec 9, 2025

Copy link
Copy Markdown
Author

@ronlieb Merged-in llvm@7675fc7. This includes all the patches since the last merge. Had to resolve two merge conflicts unrelated to the PR:

  • llvm/lib/Transforms/Vectorize/LoopVectorize.cpp: upstream inlined method
  • llvm/runtimes/CMakeLists.txt: ROCm 6.3 handling seems to be gone

The test 'flang/test/Integration/amdgpu/debug-target-var.f90' is failing. I think it is unrelated to llvm#169638

These were automatically resolved by kdiff3:

  • flang/test/Lower/OpenMP/target-enter-data-default-openmp52.f90
  • llvm/test/Transforms/LoopVectorize/AArch64/predicated-costs.ll

Had to add
74f496e

For aomp to build the amgdcn-amd-amdhsa modules, ROCm/aomp#1737 needs to be applied.

@z1-cciauto

Copy link
Copy Markdown
Collaborator

@z1-cciauto

Copy link
Copy Markdown
Collaborator

@Meinersbur Meinersbur force-pushed the users/meinersbur/rocm_flang_builtin-mods branch from 80634ae to 021eaa2 Compare December 10, 2025 14:13
@z1-cciauto

Copy link
Copy Markdown
Collaborator

@Meinersbur

Copy link
Copy Markdown
Author

This PR is intended @ronlieb to resolve a merge conflict of amd-staging with upstream, it is not intended for an additional review, but GitHub seems to have automatically added reviewers. Sorry for the noise.

@Meinersbur Meinersbur requested review from ronlieb and removed request for b-sumner, chinmaydd, david-salinas, fabianmcg, lamb-j and vangthao95 May 29, 2026 15:06
Meinersbur and others added 4 commits June 4, 2026 08:33
Before this PR, omp_lib.h is emitted to `${PREFIX}/include` or
`${PREFIX}/lib/clang/<version>/include` (install prefix) and
`${PREFIX}/runtime/src/omp_lib.h` (builddir prefix). It is never found
there because the driver only adds `${PREFIX}/include/flang/OpenMP` to
the include path.

Fix the `omp_lib.h` include by using the same mechanism as the
omp_lib.mod; that is, move it to
`${PREFIX}/lib/clang/<version>/finclude/flang/<target-triple>`. The
search path is already added by the driver via
`-fintrinsics-modules-path` by the driver. Although omp_lib.h currently
does not contain anything target-specific, it could do so in the future
and I don't think it is worth the effort to add a mechanism without the
target triple. It should also me consistent with omp_lib.mod.

The changes in detail consist of:
1. Move the omp_lib.h output in the builddir to
`${RUNTIMES_OUTPUT_RESOURCE_MOD_DIR}`. This already takes care of
whether it is a bootstrapping build or not.
3. Move the omp_lib.h install dir to
`${RUNTIMES_INSTALL_RESOURCE_MOD_PATH}`.
4. Remove the implicit search path `include/flang/OpenMP` from the
frontend. There is nothing in there anyway.
5. Hardcoding the include search path for testing to the
`LLVM_RUNTIME_TARGETS=default` build becomes unnecessary. This was way
the `include-omp-header.f90` test was still passing, although it would
not work outside the regression tests. Essentially, it tested
lit.site.cfg, not the driver.
6. Replace the old `include-omp-header.f90` test. It created a temporary
`omp_lib.h` that interferes with any other test that use `include
omp_lib.h` (there currently isn't but I originally intended to add the
replacement as an additional omp_lib.h test which resulted in a flaky
test while the original test was there). Due to how the %flang(_fc1)
substitution works, lookup order may also vary.
@Meinersbur Meinersbur force-pushed the users/meinersbur/rocm_flang_builtin-mods branch from 964b66d to 77b8814 Compare June 4, 2026 13:39
@Meinersbur Meinersbur force-pushed the users/meinersbur/rocm_flang_builtin-mods branch from 3873b24 to 64b690d Compare June 4, 2026 13:55
Meinersbur and others added 6 commits June 4, 2026 09:57
…) (llvm#171515)

Reapplication of llvm#137828 and llvm#169638

Move building the .mod files from openmp/flang to openmp/flang-rt using
a shared mechanism. Motivations to do so are:

1. Most modules are target-dependent and need to be re-compiled for each
target separately, which is something the LLVM_ENABLE_RUNTIMES system
already does. Prime example is `iso_c_binding.mod` which encodes the
target's ABI. Constants such as `c_long_double` also have different
values).
Most other modules have `#ifdef`-enclosed code as well. For instance
this caused offload targets nvptx64-nvidia-cuda/amdgpu-amd-amdhsa to use
the modules files compiled for the host which may contrain uses of the
types REAL(10) or REAL(16) not available for nvptx/amdgpu.

2. CMake has support for Fortran that we should use. Among other things,
it automatically determines module dependencies so there is no need to
hardcode them in the CMakeLists.txt.

Currently the dependencies between builtin modules and `omp_lib.mod`
seem to be incorrect:
llvm#174474 (comment)

3. It allows using Fortran itself to implement Flang-RT. Currently, only
`iso_fortran_env_impl.f90` emits object files that are needed by Fortran
applications (llvm#89403). The workaround of llvm#95388 could be reverted (PR

If using Flang for cross-compilation or target-offloading, flang-rt must
now be compiled for each target not only for the library, but also to
get the target-specific module files. For instance in a bootstrapping
runtime build, this can be done by adding:
`-DLLVM_RUNTIME_TARGETS=default;nvptx64-nvidia-cuda;amdgpu-amd-amdhsa`.

Some new dependencies come into play:
* openmp depends on flang-rt for building `lib_omp.mod` and
`lib_omp_kinds.mod`. Currently, if flang-rt is not found then the
modules are not built.
* check-flang depends on flang-rt: If not found, the majority of tests
are disabled. If not building in a bootstrpping build, the location of
the module files can be pointed to using
`-DFLANG_INTRINSIC_MODULES_DIR=<path>`, e.g. in a flang-standalone
build. Alternatively, the test needing any of the intrinsic modules
could be marked with `REQUIRES: flangrt-modules`.
* check-flang depends on openmp: Not a change; tests requiring
`lib_omp.mod` and `lib_omp_kinds.mod` those are already marked with
`openmp_runtime`.

As intrinsic are now specific to the target, their location is moved
from `include/flang` to `<resource-dir>/finclude/flang/<triple>`. The
mechnism to compute the location have been moved from flang-rt
(previously used to compute the location of `libflang_rt.*.a`) to common
locations in `cmake/GetToolchainDirs.cmake` and
`runtimes/CMakeLists.txt` so they can be used by both, openmp and
flang-rt. Potentially the mechnism could also be shared by other
libraries such as compiler-rt.

`finclude` was chosen because `gfortran` uses it as well and avoids
misuse such as `#include <flang/iso_c_binding.mod>`. The search location
is now determined by `ToolChain` in the driver, instead of by the
frontend. Another subdirectory `flang` avoids accidental inclusion of
gfortran-modules which due to compression would result in
user-unfriendly errors. Now the driver adds `-fintrinsic-module-path`
for that location to the frontend call (Just like gfortran does).
Since the driver determines the location,
tests invoking `flang -fc1` and `bbc` must also be passed the location
by llvm-lit. This works like llvm-lit does for finding the include dirs
for Clang using `-print-file-name=...`.
Before this PR, omp_lib.h is emitted to `${PREFIX}/include` or
`${PREFIX}/lib/clang/<version>/include` (install prefix) and
`${PREFIX}/runtime/src/omp_lib.h` (builddir prefix). It is never found
there because the driver only adds `${PREFIX}/include/flang/OpenMP` to
the include path.

Fix the `omp_lib.h` include by using the same mechanism as the
omp_lib.mod; that is, move it to
`${PREFIX}/lib/clang/<version>/finclude/flang/<target-triple>`. The
search path is already added by the driver via
`-fintrinsics-modules-path` by the driver. Although omp_lib.h currently
does not contain anything target-specific, it could do so in the future
and I don't think it is worth the effort to add a mechanism without the
target triple. It should also me consistent with omp_lib.mod.

The changes in detail consist of:
1. Move the omp_lib.h output in the builddir to
`${RUNTIMES_OUTPUT_RESOURCE_MOD_DIR}`. This already takes care of
whether it is a bootstrapping build or not.
3. Move the omp_lib.h install dir to
`${RUNTIMES_INSTALL_RESOURCE_MOD_PATH}`.
4. Remove the implicit search path `include/flang/OpenMP` from the
frontend. There is nothing in there anyway.
5. Hardcoding the include search path for testing to the
`LLVM_RUNTIME_TARGETS=default` build becomes unnecessary. This was way
the `include-omp-header.f90` test was still passing, although it would
not work outside the regression tests. Essentially, it tested
lit.site.cfg, not the driver.
6. Replace the old `include-omp-header.f90` test. It created a temporary
`omp_lib.h` that interferes with any other test that use `include
omp_lib.h` (there currently isn't but I originally intended to add the
replacement as an additional omp_lib.h test which resulted in a flaky
test while the original test was there). Due to how the %flang(_fc1)
substitution works, lookup order may also vary.
With llvm#201297 flang-rt-mod is required for running tests. Disable tests
by default if module files are not built.
@Meinersbur Meinersbur force-pushed the users/meinersbur/rocm_flang_builtin-mods branch from 5c80b71 to 826eb63 Compare June 5, 2026 12:50
@Meinersbur Meinersbur force-pushed the users/meinersbur/rocm_flang_builtin-mods branch from 826eb63 to ebbfdf3 Compare June 5, 2026 14:46
Meinersbur and others added 4 commits June 5, 2026 18:12
llvm#200863 added a new `-foffload-device` argument for informing the
frontend that it compiling for the device-side (and as a consequence
must not overwrite any module files compiled for the host), but the
driver was mistakenly adding `-offload-device`.

Also fix the condition and add a regression test for the driver.
@ronlieb

ronlieb commented Jun 9, 2026

Copy link
Copy Markdown
Collaborator

!PSDB

@ronlieb ronlieb merged commit 382d708 into amd-staging Jun 9, 2026
105 of 111 checks passed
@ronlieb ronlieb deleted the users/meinersbur/rocm_flang_builtin-mods branch June 9, 2026 10:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants